module SitemapSpider
  class Sitemap
    def self.boolean_opts
      {
        # add all referenced pages (set to false) to sitemap or only those we successfully downloaded (set to true)
        :add_only_valid => true,

        # maximal length of url to process - sanity check, defence against cumulative relative links etc.
        :check_max_url_length => false,

        # errors and warnings to @opts[:stderr]
        :debug => true,
        # debug downloading errors are reported with one referenced-from page if this constant is set to true
        :report_a_reference => true,
        # stop after first error
        :errors_are_fatal => false,
        # in the end, print sizes of used arrays
        :print_totals => true,

        # useful when we just want to find out if the links are available etc.
        :produce_sitemap => true,
    
        # set to false to process most recently added page in next round (lifo),
        # set to true to process the pages in the order they were added to the array (fifo)
        :process_order_is_fifo => true
      }
    end

    def self.numeric_opts
      {
        # length including 'http://' and query string
        :max_url_length => 150,

        :port => 3000,

        # seconds
        :sleep_between_requests => 0
      }
    end

    def self.string_opts
      {
        # set domain to crawl
        # be sure to use lowercase characters for hostname
        :host => '127.0.0.1',
        # be sure to use lowercase characters XXX tested only with 'http'
        :protocol => 'http',
        :start_path => '/',
    
        # should be on the same file system to replace OUTPUT_FILE atomically
        :output_file => (defined?(::Rails) ? File.join(::Rails.root.to_s, 'public', 'sitemap.xml') : 'sitemap.xml'),
        :output_tmp_file => (defined?(::Rails) ? File.join(::Rails.root.to_s, 'tmp', "sitemap.xml.#{$$}") : "sitemap.xml.#{$$}"),
    
        :input_file => (defined?(::Rails) ? File.join(::Rails.root.to_s, 'tmp', 'sitemap.in') : 'sitemap.in'),

        :lock_file => (defined?(::Rails) ? File.join(::Rails.root.to_s, 'tmp', 'sitemap.lock') : 'sitemap.lock')
      }
    end

    def self.default_opts
      boolean_opts.merge(numeric_opts).merge(string_opts)
    end
  end
end
